package com.etc.controller;



import com.etc.entity.Admin;
import com.etc.entity.Business;
import com.etc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;
@SessionAttributes(value = {"flg"})
// 登录类
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    UserService userService;

    @GetMapping("/check")
    public ModelAndView check(String username, String pwd , HttpSession session){
        ModelAndView mv  = new ModelAndView();
        //根据账号和密码去查数据库
        List<Admin> check = userService.check(username, pwd);

        if (check.size()>0){
            //登录成功
            //把登录对象存到session中
            Admin admin = check.get(0);
            session.setAttribute("loginUser",admin);

            String userCode = admin.getUserCode() ;

            session.setAttribute("username",userCode);
            //跳转到首页
            mv.setViewName("redirect:/index2.jsp");
            return mv;
        }
        //登录失败   回到登录页面
        session.setAttribute("mes","账号或密码错误");
        //重定向 到 登录页面
        mv.setViewName("redirect:/login.jsp");
        return mv;
    }
    @GetMapping("/checBbus")
    public ModelAndView checBbus(String username, String pwd , HttpSession session){
        ModelAndView mv  = new ModelAndView();
        //根据账号和密码去查数据库
        List<Business> check = userService.checkBus(username, pwd);

        if (check.size()>0){
            //登录成功
            //把登录对象存到session中
            Business admin = check.get(0);
            session.setAttribute("Bus",admin);

            //跳转到首页
            mv.setViewName("redirect:../order/quer.html?id="+username+"&osid=2");
            return mv;
        }
        //登录失败   回到登录页面
        session.setAttribute("mes","账号或密码错误");
        //重定向 到 登录页面
        mv.setViewName("redirect:/loginBus.jsp");
        return mv;
    }

    @RequestMapping("logon")
    public ModelAndView logon(String name,String id,String pwd,String pwd1){
        ModelAndView mv=new ModelAndView();
        System.out.println(id);
        System.out.println(name);
        System.out.println(pwd);
        System.out.println(pwd1);
        if (Integer.parseInt(pwd)==Integer.parseInt(pwd1)){
            if ( userService.Duplicatecheck(id)==1){
                mv.addObject("flg","账号重复");
                System.out.println("2");
                mv.setViewName("redirect:../enroll.jsp");
            }else {
                userService.logon(id,name,pwd);
                mv.addObject("flg","注册成功,请登录");
                System.out.println("1");
                mv.setViewName("redirect:../loginBus.jsp");


            }
        }else {
            mv.addObject("flg","密码错误");
            System.out.println("3");
            mv.setViewName("redirect:../enroll.jsp");
        }

return mv;




    }




}
